package com.myf.warehouse3d.controllers;

import com.myf.ApiResult;
import com.myf.authorize.AppUser;
import com.myf.authorize.JwtUtil;
import com.myf.dtos.LoginCredential;
import com.myf.dtos.LoginResult;
import com.myf.services.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/api/authorize")
@Tag(name = "授权功能", description = "授权相关API接口")
public class AuthorizeController {
    private final JwtUtil jwtUtil;
    private final UserService userService;

    public AuthorizeController(final JwtUtil jwtUtil, UserService userService) {
        this.jwtUtil = jwtUtil;
        this.userService = userService;
    }

    @Operation(summary = "登录接口")
    @PostMapping("login")
    public ApiResult login(@RequestBody LoginCredential credential, HttpServletRequest request) {
        try {
            var user = this.userService.login(credential, request.getRemoteAddr());
            var map = new HashMap<String, Object>();
            map.put("id", user.id);
            map.put("account", user.account);
            map.put("name", user.name);
            map.put("language", credential.language);
            map.put("timezone", "Asia/Shanghai");
            var result = new LoginResult();
            result.id = user.id;
            result.account = user.account;
            result.name = user.name;
            result.language = credential.language;
            result.timezone = "Asia/Shanghai";
            result.token = jwtUtil.generate(map, Integer.toString(user.id));
            return ApiResult.createSuccess(result);
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "获取出当前登录的用户信息")
    @PostMapping("userinfo")
    public ApiResult userInfo() {
        try {
            var appUser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            var result = new LoginResult();
            result.id = appUser.id;
            result.account = appUser.account;
            result.name = appUser.name;
            result.language = appUser.language;
            result.timezone = appUser.timezone;
            return ApiResult.createSuccess(result);
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

//    private static String getClientIp(HttpServletRequest request) {
//        String ip = request.getHeader("X-Forwarded-For");
//        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("Proxy-Client-IP");
//        }
//        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//        }
//        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        }
//        // 对于多个代理的情况，取第一个真实IP
//        if (ip != null && ip.contains(",")) {
//            ip = ip.split(",")[0].trim();
//        }
//        return ip;
//    }
}
